﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Caching;
using System.Web.Security;
using Telerik.Web.UI;
using System.Data.Common;
using Org.BouncyCastle.Utilities.Net;
using System.Net;
using System.Web.UI.HtmlControls;
using MSCaptcha;
using System.Configuration;
using System.DirectoryServices.AccountManagement;
using System.DirectoryServices;

public partial class Admin_Components_Member_Login : System.Web.UI.UserControl
{
   
    DatabaseHelper DB = new DatabaseHelper();
    public static bool active = false;
    public static string strID;



    
    private string IpAddress()
    {

        string strIpAddress;

        strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

        if (strIpAddress == null)

            strIpAddress = Request.ServerVariables["REMOTE_ADDR"];

        return strIpAddress;

    }

   

    private bool checkMultiLogin(string UserName)
    {
        
        return true;
    }

    protected void CheckCaptCha()
    {
     
    }

    //New
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                radCaptchaControl.Enabled = false;
                radCaptchaControl.Visible = false;
                Session["countLoginFail"] = 0;
            }
            catch (Exception ex)
            {

            }
        }
        else
        {
            if (Session["countLoginFail"] != null)
            {
                int count = int.Parse(Session["countLoginFail"].ToString());
                if (count >= 2)
                {
                    radCaptchaControl.Enabled = true;
                    radCaptchaControl.Visible = true;

                }
            }
            else
            {
                Session["countLoginFail"] = 0;
            }
        }
    }

    //Active Directory Declare

    //private bool isADAuthenticate = bool.Parse(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["IsADAuthenticate"].ConnectionString);


    
    protected void btnLogin_Authenticate(object sender, EventArgs e)
    {
        DBDataContext db = new DBDataContext();
        Session["IsFalseLogin"] = "False";
        int count = 0;
        string UserName = txtUserName.Text.Trim();
        string Password = txtPassword.Text.Trim();
        string PasswordMD5 = Utils.Encrypt(Password);

        //from p in db.BranchUsers
                // join c in db.Branches on p.BranchCode equals c.BranchCode

        var user = (from u in db.Users
                    join n in db.Employees on u.EmployeeID equals n.ID
                    where u.UserName == UserName
                    && u.Password == PasswordMD5
                    && (u.Status == 'A' || u.Status=='P') && n.Status == 'A'
                    select new
                    {
                        u.UserName,
                        n.RoleID,
                        n.ShopID,
                        n.EmployeeCode,
                        n.EmplyeeName,
                        u.Status,
                        n.ProvinceID
                    }
                   ).FirstOrDefault();
        if (user != null) // dang nhap thanh cong
        {
            Session["RoleID"] = user.RoleID.ToString();
            Session["UserName"] = user.UserName.ToString();
            Session["ShopID"] = user.ShopID.ToString();
            Session["ProvinceID"] = user.ProvinceID.ToString();
            Session["isLogin"] = true;
            Session["EmployeeCode"] = user.EmployeeCode;
            Session["EmplyeeName"] = user.EmplyeeName;
            lblFailure.Visible = false;
            lblFailure.InnerText = "";
            if (user.Status == 'P')
            {
                Session["isChangePass"] = "no";
                //Response.Redirect("~/ChangePassword");
            }
            else
            {
                Session["isChangePass"] = null;
            }
        }
        else
        {
            ControlUtil.Log("Login failure: Tài khoản " + txtUserName.Text + " chưa tồn tại trên hệ thống");
            lblFailure.Visible = true;
            lblFailure.InnerText = "Tài khoản chưa tồn tại trên hệ thống";
            count = int.Parse(Session["countLoginFail"].ToString());
            Session["countLoginFail"] = (count + 1).ToString();
            return;
        }

        if (Session["lastFunctionName"] != null)
        {
            Response.Redirect("~/" + Session["lastFunctionName"]);
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }
        /*
        Session["IsFalseLogin"] = "False";
        int count = 0;
     
        var u = (from p in db.UserRoles
                 where p.UserName == txtUserName.Text
                 select new { p.RoleID, p.Email, p.FullName }).FirstOrDefault();
        if (u != null)
        {
            Session["RoleID"] = u.RoleID.ToString();
            Session["Email"] = u.Email.ToString();
            Session["DisplayName"] = u.FullName.ToString();
        }
        else
        {
            ControlUtil.Log("Login failure: Tài khoản " + txtUserName.Text + " chưa tồn tại trên hệ thống");
            lblFailure.Visible = true;
            lblFailure.InnerText = "Tài khoản chưa tồn tại trên hệ thống";
            count = int.Parse(Session["countLoginFail"].ToString());
            Session["countLoginFail"] = (count + 1).ToString();
            return;
        }


        Session["isLogin"] = true;
        Session["UserName"] = txtUserName.Text;
        // Session["UserNameAD"] = txtUserName.Text;
        Session["DisplayName"] = txtUserName.Text;
        // Session["UserId"] = 1;
        Session["countLoginFail"] = 0;
        lblFailure.Visible = false;
        lblFailure.InnerText = "";



        var b = (from p in db.BranchUsers
                 join c in db.Branches on p.BranchCode equals c.BranchCode
                 where p.Status == 'A' && c.Status == 'A' && p.UserName == Session["UserName"].ToString()
                 select new { c.BranchCode, c.BranchName }).FirstOrDefault();
        if (b != null)
        {
            Session["BranchCode"] = b.BranchCode;
            Session["BranchName"] = b.BranchName;
        }
        if (Session["lastFunctionName"] != null)
        {
            Response.Redirect("~/" + Session["lastFunctionName"]);
        }
        else
        {
            Response.Redirect("~/Default.aspx");
        }

        */
    }
    
}







